package com.common.frame.mvp.base;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


import com.common.frame.utils.ToastUtil;

import io.reactivex.disposables.CompositeDisposable;

public abstract class BaseFragment<P extends IPresenter> extends Fragment implements IView {

    public CompositeDisposable mCompositeSubscription = new CompositeDisposable();
    private View mFragmentView;
    protected ProgressDialog mProgressDialog;

    protected boolean isPause;
    protected boolean isHide;
    public boolean isInit;
    private boolean isFirst = true;
    protected P mPresenter = null;

    /**
     * 返回注入的Presenter
     */
    public abstract P createPresenter();

    public abstract void initViews(View view);

    protected abstract void fragmentShow(boolean isFirst);

    public void refreshView(){}

    protected void initFragmentComponent(){}

    @LayoutRes
    public abstract int getLayoutId();


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initFragmentComponent();
        mPresenter = createPresenter();
        if (mPresenter != null){
            mPresenter.attachView(this, this);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mFragmentView == null) {
            mFragmentView = inflater.inflate(getLayoutId(), container, false);
            mFragmentView.setClickable(true);
            initViews(mFragmentView);
        }
        isInit = true;
        return mFragmentView;
    }

    @Override
    public void showProgress(String msg) {
        if (TextUtils.isEmpty(msg)) {
            return;
        }
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(getActivity(), ProgressDialog.STYLE_SPINNER);
        }
        mProgressDialog.setMessage(msg);
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.setCancelable(true);
        if (mProgressDialog.isShowing()) {
            return;
        }
        mProgressDialog.show();
    }

    @Override
    public void hideProgress() {
        if (mProgressDialog!= null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    public void showMsg(String message) {
        if (!TextUtils.isEmpty(message)){
            ToastUtil.showShortToast(message);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mCompositeSubscription.dispose();
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        onHiddenChanged(!isVisibleToUser);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        this.isHide = hidden;
        if (!isHide) onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        isPause = true;
    }

    @Override
    public void onResume() {
        super.onResume();
        isPause = false;
        if (isHide || !isInit){
            return;
        }
        fragmentShow(isFirst);
        isFirst = false;
    }

}
